import json
import os.path
import shutil

import requests

"""
此网站的图片是局部加载，不在首页html里面

"""


target_dir = r"D:\image\spider\率土之滨"

if os.path.exists(target_dir):
    shutil.rmtree(target_dir)
os.makedirs(target_dir)



url = "https://g0.gph.netease.com/ngsocial/community/stzb/cfg/gear_id.json?gameid=g10"
gear_response = requests.get(url)
#将请求的数据转换为jso类型
gear_datas = gear_response.json()
datas = []
for data in gear_datas:
    url_id = data.get('id')

    img_url = f"https://g0.gph.netease.com/ngsocial/community/stzb/cn/gears/gear_icon/gear_icon_{url_id}.jpg?gameid=g10"
    img_response = requests.get(img_url)
    img_path = os.path.join(target_dir,f'武器{url_id}.jpg')
    with open(img_path, "wb") as f:
        f.write(img_response.content)
    skillDecs = data.get('skillDesc')
    name = data.get('name')
    print(f"正在爬取中......,技能描述：{skillDecs} , 武器名称：{name},图片链接：{img_url}",)
    datas.append({
        "name": name,
        "skillDecs": skillDecs,
        "img_url": img_url
    })
    with open("shuaituzhibin.json", "w", encoding="utf-8") as f:
        json.dump(datas, f, ensure_ascii=False, indent=4, separators=(',', ': '))





# target_dir = r"D:\image\spider\率土之滨"
#
# if os.path.exists(target_dir):
#     shutil.rmtree(target_dir)
# os.makedirs(target_dir)
#
#
#
# url = "https://g0.gph.netease.com/ngsocial/community/stzb/cfg/hero_extra.json?gameid=g10"
# hero_response = requests.get(url)
# #将请求的数据转换为jso类型
# hero_datas = hero_response.json()
# datas = []
# for data in hero_datas:
#     url_id = data.get('id')
#
#     img_url = f"https://g0.gph.netease.com/ngsocial/community/stzb/cn/cards/cut/card_small_{url_id}.jpg?gameid=g10"
#     img_response = requests.get(img_url)
#     img_path = os.path.join(target_dir,f'人物{url_id}.jpg')
#     with open(img_path, "wb") as f:
#         f.write(img_response.content)
#
#     name = data.get('name')
#     print(f"正在爬取中......人物名称：{name},图片链接：{img_url}",)
#
#     with open("../shuaituzhibin.json", "w", encoding="utf-8") as f:
#         json.dump(datas, f, ensure_ascii=False, indent=4, separators=(',', ': '))